package cn.dingdan.controller;


import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.dingdan.pojo.Boss;
import cn.dingdan.pojo.Waiter;
import cn.dingdan.sercice.BossService;
import cn.dingdan.sercice.WaiterService;

@Controller
public class IndexController {
	
	@Resource(name="bossServiceImpl")
	private BossService bossService;
	
	@Resource(name="waiterServiceImpl")
	private WaiterService waiterService;
	
	
	
	
	@RequestMapping(value="main",method=RequestMethod.GET)
	public String indexPage(){
		return "main";
	}
	
	@RequestMapping(value="boss",method=RequestMethod.GET)
	public String xiuGai(){
		return "boss";
	}
	@RequestMapping(value="waiter",method=RequestMethod.GET)
	public String waiterLists(){
		return "waiter";
	}

	@RequestMapping(value="updaWaiter",method=RequestMethod.GET)
	public String updaWaiter_01(){
		return "updaWaiter";
	}
	@RequestMapping(value="updaWaiter1",method=RequestMethod.GET)
	public @ResponseBody Object updaWaiter(@RequestParam(value="waiter",required=false)Integer waiter
							,HttpSession session){
		Map<String,Object> map = new HashMap<String, Object>();
		session.setAttribute("waiter", waiter);
		map.put("url", "/updaWaiter");
		return map;
	}

	@RequestMapping(value="showWaiter",method=RequestMethod.GET)
	public String showWaiter(){
		return "showWaiter";
	}

	@RequestMapping(value="updatePwd",method=RequestMethod.POST)
	public @ResponseBody Object updatePwd(@RequestParam(required=false,value="waiterId")Integer waiterId,
							@RequestParam(required=false,value="waiterPassword")String waiterPassword,
							@RequestParam(required=false,value="waiterPassword1")String waiterPassword1){
		Map<String, Object> map = new HashMap<String, Object>();
		if (waiterPassword.equals(waiterPassword1) && !"".equals(waiterPassword1) && waiterPassword1!=null) {
			Waiter waiter =new Waiter();
			waiter.setWaiterId(waiterId);
			waiter.setWaiterPassword(waiterPassword1);
			int num = waiterService.updateWaiter(waiter);
			if (num>0) {
				map.put("num", num);
			}
		}
		return map;
	}
	@RequestMapping(value="delWaiter",method=RequestMethod.GET)
	public @ResponseBody Object delWaiter(@RequestParam(required=false,value="waiterId")Integer waiterId){
		int num = waiterService.delWaiter(waiterId);
		Map<String, Object> map = new HashMap<String, Object>();
		if (num>0) {
			map.put("num",num);
		}
		return map;
	}
	
	@RequestMapping(value="updaBossPwd",method=RequestMethod.POST)
	public @ResponseBody Object updaBossPwd(
		   @RequestParam(value="userId",required=false)Integer userId,
		   @RequestParam(value="userPassword",required=false)String userPassword,
		   @RequestParam(value="userPassword_01",required=false)String userPassword_01,
		   @RequestParam(value="userPassword_02",required=false)String userPassword_02
		   ){
		Map<String, Object> map = new Hashtable<String, Object>();
		Boss boss_01=bossService.getBossPad(userId,userPassword);
		if (boss_01!=null && userPassword_01.equals(userPassword_02) && (!"".equals(userPassword_01))) {
			Boss boss = new Boss();
			boss.setUserId(userId);
			boss.setUserPassword(userPassword_02);
			int num=bossService.updateBoss(boss);
			map.put("suceess", num>0);
			if (num>0) {
				map.put("url", "/login");
			}
		}
		return map;
	}
	
	@RequestMapping(value="getWaiter",method=RequestMethod.GET)
	public String getWaiter(HttpSession session){
		List<Waiter> waiters = waiterService.getLists();
		session.setAttribute("waiters", waiters);
		return "showWaiter";
	}
	@RequestMapping(value="addWaiter",method=RequestMethod.POST)
	public @ResponseBody Object addWaiter(
		   @RequestParam(value="waiterCode",required=false) String waiterCode,
		   @RequestParam(value="waiterName",required=false) String waiterName,
		   @RequestParam(value="waiterPassword",required=false) String waiterPassword
						){
		Waiter waiter = new Waiter();
		Map<String,Object> map = new HashMap<String, Object>();
		if (!"".equals(waiterCode) && !"".equals(waiterName) && !"".equals(waiterPassword)) {
			waiter.setWaiterCode(waiterCode);
			waiter.setWaiterName(waiterName);
			waiter.setWaiterPassword(waiterPassword);
			int num = waiterService.addWaiter(waiter);
			if (num>0) {
				map.put("num", num);
			}
		}
		return map;
	}
}
